package com.sright.bcard.content.biz.service;

import com.sright.bcard.common.service.share.dto.page.PageParamRequest;
import com.sright.bcard.common.service.share.dto.pager.Pager;
import com.sright.bcard.common.service.share.utils.DataConvertUtils;
import com.sright.bcard.content.biz.manager.FeedbackManager;
import com.sright.bcard.content.dal.dao.FeedbackDAO;
import com.sright.bcard.content.dal.entity.FeedbackDO;
import com.sright.bcard.common.service.share.dto.content.FeedbackDto;
import com.sright.bcard.content.share.request.FeedBackParamRequest;
import com.sright.bcard.content.share.service.FeedbackService;
import com.sright.bcard.user.dal.dao.MemberDAO;
import com.sright.bcard.user.dal.entity.MemberDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: lumf
 * Date: 2020/8/13 7:50 PM
 */
@Slf4j
@Component
public class FeedbackServiceImpl implements FeedbackService {
    @Autowired
    FeedbackDAO feedbackDAO;

    @Autowired
    MemberDAO memberDAO;

    @Autowired
    FeedbackManager feedbackManager;

    @Override
    public Pager<FeedbackDto> getFeedbackList(Integer pageIndex, Integer pageSize,
                                             @NotNull String nickName, Integer status,
                                             Long beginTime, Long endTime) {
        MemberDO memberDO = null;
        if (StringUtils.isNotEmpty(nickName)) {
            memberDO = memberDAO.selectMemberIdByNickName(nickName);
        }

        PageParamRequest paramRequest = new PageParamRequest();
        Integer offset = (pageIndex - 1) * pageSize;
        paramRequest.setOffset(offset);
        paramRequest.setLimit(pageSize);
        Integer count = feedbackDAO.count(memberDO != null ? memberDO.getId() : null);
        List<FeedbackDO> boList = feedbackDAO.pageList(beginTime, endTime, status, paramRequest);
        List<FeedbackDto> dtoList = new ArrayList<>();
        DataConvertUtils.convertDOList2DTOList(boList, dtoList, FeedbackDto.class);
        for (FeedbackDto dto : dtoList) {
            dto.setNickName(memberDO != null ? memberDO.getNickname() : null);
        }
        Pager pager = new Pager(pageIndex, pageSize, count, dtoList);
        return pager;
    }

    @Override
    public boolean updateStatus(Long feedbackId, Integer usefulStatus) {
        boolean success = feedbackDAO.updateStatus(feedbackId, usefulStatus);
        return success;
    }

    @Override
    public Long addFeedback(FeedBackParamRequest feedBackParamRequest) {
        return feedbackManager.addFeedback(feedBackParamRequest);
    }
}
